#ifndef __LEOS_THREAD_H__
#define __LEOS_THREAD_H__

#include "leos_def.h"
#include "leos_obj.h"
#include "leos_timer.h"
#include "leos_conf.h"

struct le_thread {
    le_obj_t        base;
    le_list_t       tlist;

    void           *sp;
    void           *entry;
    void           *parameter;
    void           *stack_addr;
    le_uint32_t     stack_size;

    le_uint8_t      state;

    le_uint8_t      current_priority;
    le_uint8_t      init_priority;

    le_int32_t      init_tick;
    le_int32_t      remaining_tick;

    struct le_timer thread_timer;

    void (*clean_up)(struct le_thread *self);
};

typedef struct le_thread *le_thread_t;

extern le_thread_t  le_current_thread;
extern le_list_t    le_thread_priority_table[LE_THREAD_PRIORITY_MAX];

void le_thread_init(le_thread_t     thread,
                    const char     *name,
                    void (*entry)(void *parameter),
                    void           *parameter,
                    void           *stack_start,
                    le_uint32_t     stack_size,
                    le_uint8_t      priority,
                    le_int32_t      tick);
void le_thread_yield(void);
void le_thread_startup(le_thread_t thread);
void le_thread_mdelay(le_uint32_t ms);
void le_thread_suspend(le_thread_t thread);
void le_thread_resume(le_thread_t thread);

void le_thread_idle_init(void);

void le_system_scheduler_init(void);
void le_system_scheduler_start(void);
void le_schedule_insert_thread(le_thread_t thread);
void le_schedule_remove_thread(le_thread_t thread);
void le_schedule(void);

#endif

